package itstudio.instructor.fragment;

import itstudio.app.wq.R;
import itstudio.instructor.adapter.MessageAdapter;
import itstudio.instructor.config.Config;
import itstudio.instructor.entity.Comment;
import itstudio.instructor.http.TwitterRestClient;
import itstudio.instructor.ui.MessageDetailActivity;
import itstudio.instructor.util.LoginUtil;
import itstudio.instructor.util.SharedPreferencesUtil;
import itstudio.instructor.util.TimeUtil;
import itstudio.instructor.xlistview.XListView;
import itstudio.instructor.xlistview.XListView.IXListViewListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.Header;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nhaarman.listviewanimations.swinginadapters.AnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.ScaleInAnimationAdapter;

@SuppressLint("ValidFragment")
public class FragmentMyReply extends Fragment implements IXListViewListener{

	//view
	private View loadingLayout;
    private View loadNoneLayout;
	private XListView xListView;
	private MessageAdapter adapter;
	private View replaceView;
	private List<Comment> commentList;
	private int page = 0;
	private Context context;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = getActivity();

	}
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (replaceView == null) {
			replaceView = inflater.inflate(R.layout.fragment_message_listview, null);
			initView();
		}
		ViewGroup parent = (ViewGroup) replaceView.getParent();
		if (parent != null) {
			parent.removeView(replaceView);
		}

		return replaceView;
	}

	
	private void initView() {
		// TODO Auto-generated method stub
		commentList = new ArrayList<Comment>();
		xListView = (XListView) replaceView.findViewById(R.id.message_listview); 
		loadingLayout =  replaceView.findViewById(R.id.view_loading);
		loadNoneLayout =  replaceView.findViewById(R.id.view_load_nodata);
		adapter = new MessageAdapter(context, commentList,R.layout.listitem_my_reply); 
		AnimationAdapter animAdapter = new ScaleInAnimationAdapter(adapter);
		xListView.setPullLoadEnable(true);
		animAdapter.setAbsListView(xListView);
		animAdapter.setInitialDelayMillis(100);
		xListView.setAdapter(animAdapter);
		xListView.setAdapter(adapter);
		xListView.setXListViewListener(this);
		xListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				TextView text=(TextView) arg1.findViewById(R.id.message_content);
				text.setTextColor(getResources().getColor(R.color.light_black));
				Intent intent = new Intent();
				intent.setClass(context, MessageDetailActivity.class);
				Comment comment = (Comment) adapter.getItem(position-1);
				intent.putExtra("comment", comment);
				context.startActivity(intent);

			}
		});
		loadData(1,true);
	}

	//加载
	private void loadData(int page_num,final boolean refesh){
		RequestParams params = new RequestParams();
		params.put("page", page_num);
		TwitterRestClient.post(Config.AC_COMMENT_MY_REPLY, params, new AsyncHttpResponseHandler(){

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				onLoad();
				loadingLayout.setVisibility(View.GONE);
				loadNoneLayout.setVisibility(View.GONE);
			}

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] data) {
				//加载成功
				onLoad();
				loadingLayout.setVisibility(View.GONE);
				loadNoneLayout.setVisibility(View.GONE);
				Gson gson = new GsonBuilder()  
				  .setDateFormat("yyyy-MM-dd HH:mm:ss")  
				  .create();
				String response = new String(data);
				System.out.println(response);
				if(response.indexOf(Config.LOGIN)!=-1 && response.length()<12){
					LoginUtil.Login(context);
				}
				// 有数据
				else {
					commentList  = gson.fromJson( new String(data), new TypeToken<List<Comment>>(){}.getType());
					page = adapter.appendData(commentList,refesh);
					System.out.println(page);
					if(page==0){
						loadNoneLayout.setVisibility(View.VISIBLE);
					}
					if(commentList!=null  && commentList.size()==10){
						xListView.setPullLoadEnable(true);
					}
					else{
						xListView.setPullLoadEnable(false);
					}
					
				}

			}});
	}

	
	// 设置下拉刷新时间
		@Override
		public void onRefresh() {
			// TODO Auto-generated method stub
			loadData(1,true);
			String curDate = TimeUtil
					.dateToString(new Date(), TimeUtil.FORMAT_MONTH_DAY_TIME_EN);
			SharedPreferencesUtil.add(context, "myreply_time", curDate);

		}

		// 设置loadmore刷新时间
		@Override
		public void onLoadMore() {
			// TODO Auto-generated method stub
			loadData(page+1,false);
		}

		private void onLoad() {
			xListView.stopRefresh();
			xListView.stopLoadMore();

			String curDate = SharedPreferencesUtil.get(context, "myreply_time");
			if(!"".equals(curDate)){
				xListView.setRefreshTime(curDate);
			}else{
				curDate = TimeUtil
						.dateToString(new Date(), TimeUtil.FORMAT_MONTH_DAY_TIME_EN);
				xListView.setRefreshTime(curDate);
			}
		}

}